package com.bestv.search.common.dao.hibernate;

import com.bestv.search.common.dao.ProgramDao;
import com.bestv.search.common.model.Program;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ProgramDaoHibernate extends GenericDaoHibernate<Program, Long> implements ProgramDao {

    public ProgramDaoHibernate() {
        super(Program.class);
    }

    public void saveProgramList(List<Program> list) {
        super.saveObjectList(list);
    }

    @SuppressWarnings("unchecked")
    public List<Program> getProgramByCode(String code) {
        List<Program> list = getHibernateTemplate().find("from Program p where p.code = ?", code);
        return list;
    }

    @SuppressWarnings("unchecked")
    public List<Program> getNewEngineSeriesByUpdateTime(Date updateTime) {
        SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String hql = "from Program where updateTime>= '" + timeformat.format(updateTime)
                + "' and (regexp(name, '.+\\\\[[0-9]{1,}\\\\]new')=1 or regexp(name, '.+\\\\[[0-9]{1,}\\\\]')=1) "
                + " and updateFlag<>2 order by name";
        return getHibernateTemplate().find(hql);
    }

    @SuppressWarnings({"rawtypes"})
    public Program getProgramById(long id) {
        String hql = "from Program where id= " + id;
        List list = getHibernateTemplate().find(hql);
        return list == null || list.size() == 0 ? null : (Program) list.get(0);
    }
}
